package org.kamionowski.supreme.jobs;

import org.apache.commons.lang.Validate;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Marcin Kamionowski
 * Date: 11.04.11
 * Time: 22:43
 */
public class ScanContext {

    private final ScanRequest request;
    private Integer percentageOfScan = null;
    private boolean ended = false;
    private List<JobEndListener> listeners = new LinkedList<JobEndListener>();

    public ScanContext(ScanRequest request) {
        Validate.notNull(request);
        this.request = request;
    }

    public ScanRequest getRequest() {
        return request;
    }

    public boolean isEnded() {
        return ended;
    }

    public void setEnded(boolean ended) {
        changeable();
        this.ended = ended;
        for(JobEndListener listener : listeners){
            listener.jobEnd(this);
        }
    }

    public void addListener(JobEndListener listener){
        this.listeners.add(listener);
    }

    public void setPercentageOfScan(Integer percentageOfScan) {
        changeable();
        this.percentageOfScan = percentageOfScan;
    }

    public Integer getPercentageOfScan() {
        return percentageOfScan;
    }

    private void changeable(){
        Validate.isTrue(!ended, "Cannot change context after end.");
    }

}
